Skip to content

修复:支持带 slug 的帖子 URL 解析#5

Open
joy717 wants to merge 1 commit into
linux-do:masterfrom
joy717:fix/topic-url-slug-parsing
Open

修复:支持带 slug 的帖子 URL 解析#5
joy717 wants to merge 1 commit into
linux-do:masterfrom
joy717:fix/topic-url-slug-parsing

Conversation

@joy717

@joy717 joy717 commented Jun 17, 2026

Copy link
Copy Markdown

问题

ForumTopicInfo.from_url 的正则只匹配 /t/topic/{id} 形式:

pattern = r"/t/topic/(\d+)(?:/\d+)?"

Discourse 标准的分享链接会带 slug,形如 /t/{slug}/{id}(例如 ASCII 标题的帖子)。这类 URL 直接粘进来会解析失败,抛出 无法从URL中解析出主题ID

复现:

MATCH  https://linux.do/t/topic/123456
FAIL   https://linux.do/t/some-giveaway-slug/123456   ← 带 slug 的标准分享链接

修改

把正则放宽为:

pattern = r"/t/(?:[^/]+/)?(\d+)(?:/\d+)?"

slug 段变为可选,同时兼容两种形式,严格更宽松,不影响原有 /t/topic/{id} 的解析。

OK  123456  /t/topic/123456
OK  123456  /t/some-giveaway-slug/123456     ← 现在可用
OK  123456  /t/123456

说明

边界情况:无 slug 的 /t/{id}/{post} 会把 post 号误当成 id —— 但 Discourse 的规范 URL 一般都带 slug,实际不会出现。

🤖 Generated with Claude Code

The regex only matched the `/t/topic/{id}` form, so standard Discourse
share links that carry a slug (`/t/{slug}/{id}`) failed with
"无法从URL中解析出主题ID".

Broaden the pattern to `/t/(?:[^/]+/)?(\d+)(?:/\d+)?` so it accepts both
the slug form and the existing `/t/topic/{id}` form. Strictly more
permissive — no regression on the current case.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@joy717

joy717 commented Jun 18, 2026

Copy link
Copy Markdown
Author

@wozulong hi, could you review this pr? thanks
btw, there are other prs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant